package com.dietmanager.view;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.dietmanager.datalayer.DataLayer;
import com.dietmanager.model.FoodModel;
import com.dietmanager.model.Recipe;
import com.dietmanager.model.javafx.util.RecipeListEntry;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;

/**
 * 
 * @author Danijel, Petar
 * @description AddFoodController is used as a separate window which user uses
 *              to control adding, creating and editing food.
 *
 */

public class AddRecipeView extends Observable {

	ArrayList<Observer> observerList = new ArrayList<Observer>();

	private final ObservableList<RecipeListEntry> data = FXCollections
			.observableArrayList();

	// variables and objects
	public Button add, save;
	public ComboBox foodRecipeComboBox;
	public TextField amountTextField, nameTextField;
	public VBox addRecipeView;
	public TableView recipeTable;
	public TableColumn food, amount, calories, fat, carbohydrates, proteins;

	public ObservableList<String> recipeFoodList = FXCollections
			.observableArrayList();

	public AddRecipeView() {

		createAddRecipeView();
	}

	public void createAddRecipeView() {
		addRecipeView = new VBox();
		addRecipeView.setSpacing(10);
		addRecipeView.getStyleClass().add("addRecipeView");

		// Create the table for food and amount
		recipeTable = new TableView();
		food = new TableColumn("Food");
		amount = new TableColumn("Amount");
		calories = new TableColumn("Calories");
		fat = new TableColumn("Fat");
		carbohydrates = new TableColumn("Carbohydrates");
		proteins = new TableColumn("Proteins");

		food.setCellValueFactory(new PropertyValueFactory<RecipeListEntry, String>(
				"food"));
		calories.setCellValueFactory(new PropertyValueFactory<RecipeListEntry, String>(
				"calories"));
		amount.setCellValueFactory(new PropertyValueFactory<RecipeListEntry, String>(
				"amount"));

		recipeTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
		recipeTable.getColumns().addAll(food, amount, calories, fat,
				carbohydrates, proteins);
		recipeTable.getStyleClass().add("recipeTable");

		// Create HBox that contains adding elements
		HBox addBar = new HBox();
		addBar.setSpacing(7);
		addBar.setAlignment(Pos.CENTER);
		// Create combobox, textfield, button
		foodRecipeComboBox = new ComboBox(recipeFoodList);
		foodRecipeComboBox.setValue("Pick a Food/Recipe");

		recipeTable.setItems(data);

		amountTextField = new TextField();
		amountTextField.setPrefColumnCount(2);
		amountTextField.setPromptText("#N.");

		Recipe r = new Recipe("", 0);

		add = new Button("Add Food");
		add.getStyleClass().add("customButton");
		add.setOnAction(new EventHandler<ActionEvent>() {

			// When add button pressed...
			@Override
			public void handle(ActionEvent event) {
				DataLayer<ArrayList<FoodModel>> foodDataLayer = new DataLayer<ArrayList<FoodModel>>(
						FoodModel.class.getName());
				ArrayList<FoodModel> foodModelArr = foodDataLayer
						.readFile();

				FoodModel fm = null;
				for (FoodModel f : foodModelArr)

					if (f.getName().equals(
							foodRecipeComboBox.getValue().toString()))
						fm = f;

				if (fm != null) {
					try {
						fm.setAmount(Integer.parseInt(amountTextField.getText()
								.toString()));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					r.add(fm);
					data.add(new RecipeListEntry(fm.getName(), fm.getCalories()
							+ "", fm.getAmount() + ""));
				}
			}

		}); // end of add button action

		// add everything to hbox
		addBar.getChildren().addAll(foodRecipeComboBox, amountTextField, add);

		// Bottom textfield and button
		HBox bottomItems = new HBox(); // just to keep it more clear and easier
		// to manipulate
		bottomItems.setSpacing(7);
		bottomItems.setAlignment(Pos.CENTER);

		nameTextField = new TextField();
		nameTextField.setPromptText("Food/Recipe name");
		save = new Button("Save");
		save.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				r.setName(nameTextField.getText().toString());
				notifyObservers(r);

				data.clear();
			}

		}); // end of save button action
		bottomItems.getChildren().addAll(nameTextField, save);

		// Add everything to GridPane
		addRecipeView.getChildren().addAll(recipeTable, addBar, bottomItems);

		return;
	}

	public VBox getAddRecipeView() {
		return addRecipeView;
	}

	public void updateFoodList(ArrayList<FoodModel> fm) {
		recipeFoodList.clear();

		if (fm != null)
			for (FoodModel f : fm)
				recipeFoodList.add(f.getName());
	}

	public void addObserver(Observer o) {
		observerList.add(o);
	}

	public void addObserverCollection(ArrayList<Observer> obs) {
		//observerList.clear();
		observerList = obs;
	}

	public void notifyObservers(Object o) {
		for (Observer ob : observerList)
			ob.update(this, o);
	}

}